﻿<?php

if(!defined("INCLUDE")) die("Nedostatečné oprávnění pro spouštění souboru.");

# --------------------------------------------------------------------------------------------------------------------------
# CLASS ENTITY
# --------------------------------------------------------------------------------------------------------------------------
# Třída entity se používá jako přepravní kontejner pro data o článku. Je zde řešeno ohlídání proti SQL injekci, některé
# get metody také upravujou výstup dat, čili třeba z datumu lze získat vlastní formát.
#
# --------------------------------------------------------------------------------------------------------------------------
# INFORMACE O SOUBORU
# --------------------------------------------------------------------------------------------------------------------------
# Autor: Zechy (editoval Romop5)
# Poslední editace: 1. 12. 2012

class entity
{
	private $id;
	private $autor;
	private $heading;
	private $content;
	private $datetime;
	private $sql;

	public function __construct()
	{
		$this->sql = new sqlquery;
	}

	public function setAll_fromDB($data)
	{
		if(isset($data["id"])) $this->id = $data["id"];

		$this->autor = $data["autor"];
		$this->heading = $data["heading"];
		$this->content = $data["content"];

		if(isset($data["datetime"])) $this->datetime = $data["datetime"];
	}

	public function setAll($data)
	{
		$this->sql->create_connect();

		if(isset($data["id"])) $this->id = mysql_real_escape_string($data["id"]);
		$this->autor = mysql_real_escape_string($data["autor"]);
		$this->heading = mysql_real_escape_string($data["heading"]);
		$this->content = mysql_real_escape_string($data["content"]);

		if(empty($data["date"]) && empty($data["time"])) $this->datetime = date("Y-m-d H:i:s");
		else if(isset($data["date"]) && isset($data["time"])) $this->datetime = $data["date"]." ".$data["time"];

		$this->sql->close_connect();
	}

	public function set_id($id)
	{
		$this->sql->create_connect();

		$this->id = mysql_real_escape_string($id);

		$this->sql->close_connect();
	}
	public function set_autor($autor)
	{
		$this->sql->create_connect();

		$this->autor = mysql_real_escape_string($autor);

		$this->sql->close_connect();
	}
	public function set_heading($heading)
	{
		$this->sql->create_conect();

		$this->heading = mysql_real_escape_string($heading);

		$this->sql->close_conect();
	}
	public function set_content($content)
	{
		$this->sql->create_conect();

		$this->content = mysql_real_escape_string($content);

		$this->sql->close_conect();
	}
	public function set_datetime($datetime)
	{
		$this->sql->create_conect();

		$this->datetime = mysql_real_escape_string($datetime);

		$this->sql->close_conect();
	}
	public function get_id()
	{
		return $this->id;
	}
	public function get_autor()
	{
		return $this->autor;
	}
	public function get_heading()
	{
		return $this->heading;
	}
	public function get_content()
	{
		return $this->content;
	}
	public function get_datetime($what = "both", $format = null)
	{
		$formated;
		
		if(!is_null($format))
		{
			$formated = date($format, strtotime($this->datetime));
		}

		if($what == "both")
		{
			return $this->datetime;
		}
		else if($what == "date")
		{
			$datetime = explode(" ", $this->datetime);
			return $datetime[0];
		}
		else if($what == "time")
		{
			$datetime = explode(" ", $this->datetime);
			return $datetime[1];
		}
		else if($what == "formated")
		{
			return $formated;
		}
	}

}

?>